<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">

    <title>生态瞬时评估 - 主页</title>

    <meta name="keywords" content="">
    <meta name="description" content="">

    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->

    <link rel="shortcut icon" href="../favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <h3 class="col-sm-offset-3 col-sm-6 text-center">添加实验者</h3>
    </div>
    <div class="row">
        <div class="col-sm-offset-3 col-sm-6">
            <form class="form-horizontal" style="margin-top: 20px">
                <div class="form-group">
                    <label class="col-sm-3 control-label">实验名称：</label>
                    <div class="col-sm-8">
                        <span class="form-control" style="border:none;" id="test-name">心理实验</span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="uname" class="col-sm-3 control-label">用户名：</label>
                    <div class="col-sm-8">
                        <input type="text" placeholder="用户名称" class="form-control" id="uname" list="unameList">
                        <datalist id="unameList">

                        </datalist>
                        <input type="hidden" value="" id="userIdBox">
                    </div>
                    <!--<div class="col-sm-2">-->
                        <!--<input type="button" class="btn btn-primary btn-block" value="搜索">-->
                    <!--</div>-->
                </div>
                <div class="col-sm-offset-3 col-sm-8">
                    <input type="button" id="addBtn" value="添加" class="btn btn-primary btn-block">
                </div>
            </form>
        </div>
    </div>

</div>




<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<script>
    $(document).ready(function () {
        $("#test-name").text(sessionStorage.getItem("testName"));
        var token = sessionStorage.getItem("token");
        var testId = sessionStorage.getItem("testId");
        $.ajax({
            type:"GET",
            url:"http://123.206.18.189:8080/users",
            datatype:"json",
            headers:{
                authToken:token
            },
            data:{role:"MANAGER"},
            success:function (res) {
                var html = "";
                for(var i=0;i<res.length;i++){
                    html +=    `<option value="${res[i].realName}" data-id="${res[i].id}">`
                }
                $("#unameList").html(html);
            }
        })
        //获取datalst选取值的其他属性值
        $("#uname").on("input",function(){
            var $options=$("#unameList").children();
            for(var i=0;i<$options.length;i++){
                if($options.eq(i).val().trim()==$("#uname").val().trim()){
                    $("#userIdBox").val($options.eq(i).attr("data-id"));
                    break;
                }
            }
        });





        $("#addBtn").click(function () {
            var userId = $("#userIdBox").val();
            $.ajax({
                type:"POST",
                url:"http://123.206.18.189:8080/experiments/"+testId+"/experiment_administrators",
                headers:{
                    authToken:token
                },
                contentType:"application/json",
                data:JSON.stringify({"experimentId":testId, "userId":userId}),
                complete:function (xhr) {
                    if(xhr.status==200&&xhr.readyState==4){
                        alert("实验者添加成功！");
                        history.back();
                    }

                }
            })
        })
    })

</script>

</body>
</html>